package com.example.duanlei417.mypro2.view;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;

import com.example.duanlei417.mypro2.R;
import com.example.duanlei417.mypro2.view.utils.VisibilityCheckUtil;

public class TestPVActivity extends AppCompatActivity {

    ScrollView mScrollView;
    TestProgressBar mMyPro;

    boolean isShow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_pv);

        mMyPro = findViewById(R.id.ppb_my_pro);

//        mMyPro.setProgressWithAnim(60, 5000);

        mScrollView = findViewById(R.id.sv_test);
        mScrollView.getViewTreeObserver().addOnScrollChangedListener(
                new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {

                float ratio = VisibilityCheckUtil.check(mMyPro, mScrollView);

                if (ratio > 0.5) {
                    if (!isShow) {
                        mMyPro.setProgressWithAnim(80, 2000);
                    }
                    //mScrollView.getViewTreeObserver().removeOnScrollChangedListener(this);
                    isShow = true;
                }
                if (isShow && ratio == 0) {
                    isShow = false;
                    //mScrollView.getViewTreeObserver().addOnScrollChangedListener(this);
                }
            }
        });

        mMyPro.post(new Runnable() {
            @Override
            public void run() {
                if (mMyPro.getGlobalVisibleRect(new Rect())) {
                    mMyPro.setProgressWithAnim(80, 2000);
                    isShow = true;
                }
            }
        });

    }
}
